package com.qipay.baas.mapper;

import static org.apache.ibatis.jdbc.SqlBuilder.BEGIN;
import static org.apache.ibatis.jdbc.SqlBuilder.DELETE_FROM;
import static org.apache.ibatis.jdbc.SqlBuilder.FROM;
import static org.apache.ibatis.jdbc.SqlBuilder.INSERT_INTO;
import static org.apache.ibatis.jdbc.SqlBuilder.ORDER_BY;
import static org.apache.ibatis.jdbc.SqlBuilder.SELECT;
import static org.apache.ibatis.jdbc.SqlBuilder.SELECT_DISTINCT;
import static org.apache.ibatis.jdbc.SqlBuilder.SET;
import static org.apache.ibatis.jdbc.SqlBuilder.SQL;
import static org.apache.ibatis.jdbc.SqlBuilder.UPDATE;
import static org.apache.ibatis.jdbc.SqlBuilder.VALUES;
import static org.apache.ibatis.jdbc.SqlBuilder.WHERE;

import com.qipay.baas.model.Sp;
import com.qipay.baas.model.SpExample.Criteria;
import com.qipay.baas.model.SpExample.Criterion;
import com.qipay.baas.model.SpExample;
import java.util.List;
import java.util.Map;

public class SpSqlProvider {

    public String deleteByExample(SpExample example) {
        BEGIN();
        DELETE_FROM("sp");
        applyWhere(example, false);
        return SQL();
    }

    public String insertSelective(Sp record) {
        BEGIN();
        INSERT_INTO("sp");
        
        if (record.getMchId() != null) {
            VALUES("mch_id", "#{mchId,jdbcType=VARCHAR}");
        }
        
        if (record.getCardNo() != null) {
            VALUES("card_no", "#{cardNo,jdbcType=VARCHAR}");
        }
        
        if (record.getDeleteFlag() != null) {
            VALUES("delete_flag", "#{deleteFlag,jdbcType=TINYINT}");
        }
        
        if (record.getName() != null) {
            VALUES("`name`", "#{name,jdbcType=VARCHAR}");
        }
        
        if (record.getService() != null) {
            VALUES("service", "#{service,jdbcType=VARCHAR}");
        }
        
        if (record.getBank() != null) {
            VALUES("bank", "#{bank,jdbcType=VARCHAR}");
        }
        
        if (record.getFactoryClassName() != null) {
            VALUES("factory_class_name", "#{factoryClassName,jdbcType=VARCHAR}");
        }
        
        if (record.getUpdateDate() != null) {
            VALUES("update_date", "#{updateDate,jdbcType=TIMESTAMP}");
        }
        
        if (record.getCreateDate() != null) {
            VALUES("create_date", "#{createDate,jdbcType=TIMESTAMP}");
        }
        
        if (record.getState() != null) {
            VALUES("`state`", "#{state,jdbcType=TINYINT}");
        }
        
        if (record.getFailReason() != null) {
            VALUES("fail_reason", "#{failReason,jdbcType=VARCHAR}");
        }
        
        if (record.getSpUserName() != null) {
            VALUES("sp_user_name", "#{spUserName,jdbcType=VARCHAR}");
        }
        
        if (record.getZfbNo() != null) {
            VALUES("zfb_no", "#{zfbNo,jdbcType=VARCHAR}");
        }
        
        return SQL();
    }

    public String selectByExample(SpExample example) {
        BEGIN();
        if (example != null && example.isDistinct()) {
            SELECT_DISTINCT("id");
        } else {
            SELECT("id");
        }
        SELECT("mch_id");
        SELECT("card_no");
        SELECT("delete_flag");
        SELECT("`name`");
        SELECT("service");
        SELECT("bank");
        SELECT("factory_class_name");
        SELECT("update_date");
        SELECT("create_date");
        SELECT("`state`");
        SELECT("fail_reason");
        SELECT("sp_user_name");
        SELECT("zfb_no");
        FROM("sp");
        applyWhere(example, false);
        
        if (example != null && example.getOrderByClause() != null) {
            ORDER_BY(example.getOrderByClause());
        }
        
        return SQL();
    }

    public String updateByExampleSelective(Map<String, Object> parameter) {
        Sp record = (Sp) parameter.get("record");
        SpExample example = (SpExample) parameter.get("example");
        
        BEGIN();
        UPDATE("sp");
        
        if (record.getId() != null) {
            SET("id = #{record.id,jdbcType=BIGINT}");
        }
        
        if (record.getMchId() != null) {
            SET("mch_id = #{record.mchId,jdbcType=VARCHAR}");
        }
        
        if (record.getCardNo() != null) {
            SET("card_no = #{record.cardNo,jdbcType=VARCHAR}");
        }
        
        if (record.getDeleteFlag() != null) {
            SET("delete_flag = #{record.deleteFlag,jdbcType=TINYINT}");
        }
        
        if (record.getName() != null) {
            SET("`name` = #{record.name,jdbcType=VARCHAR}");
        }
        
        if (record.getService() != null) {
            SET("service = #{record.service,jdbcType=VARCHAR}");
        }
        
        if (record.getBank() != null) {
            SET("bank = #{record.bank,jdbcType=VARCHAR}");
        }
        
        if (record.getFactoryClassName() != null) {
            SET("factory_class_name = #{record.factoryClassName,jdbcType=VARCHAR}");
        }
        
        if (record.getUpdateDate() != null) {
            SET("update_date = #{record.updateDate,jdbcType=TIMESTAMP}");
        }
        
        if (record.getCreateDate() != null) {
            SET("create_date = #{record.createDate,jdbcType=TIMESTAMP}");
        }
        
        if (record.getState() != null) {
            SET("`state` = #{record.state,jdbcType=TINYINT}");
        }
        
        if (record.getFailReason() != null) {
            SET("fail_reason = #{record.failReason,jdbcType=VARCHAR}");
        }
        
        if (record.getSpUserName() != null) {
            SET("sp_user_name = #{record.spUserName,jdbcType=VARCHAR}");
        }
        
        if (record.getZfbNo() != null) {
            SET("zfb_no = #{record.zfbNo,jdbcType=VARCHAR}");
        }
        
        applyWhere(example, true);
        return SQL();
    }

    public String updateByExample(Map<String, Object> parameter) {
        BEGIN();
        UPDATE("sp");
        
        SET("id = #{record.id,jdbcType=BIGINT}");
        SET("mch_id = #{record.mchId,jdbcType=VARCHAR}");
        SET("card_no = #{record.cardNo,jdbcType=VARCHAR}");
        SET("delete_flag = #{record.deleteFlag,jdbcType=TINYINT}");
        SET("`name` = #{record.name,jdbcType=VARCHAR}");
        SET("service = #{record.service,jdbcType=VARCHAR}");
        SET("bank = #{record.bank,jdbcType=VARCHAR}");
        SET("factory_class_name = #{record.factoryClassName,jdbcType=VARCHAR}");
        SET("update_date = #{record.updateDate,jdbcType=TIMESTAMP}");
        SET("create_date = #{record.createDate,jdbcType=TIMESTAMP}");
        SET("`state` = #{record.state,jdbcType=TINYINT}");
        SET("fail_reason = #{record.failReason,jdbcType=VARCHAR}");
        SET("sp_user_name = #{record.spUserName,jdbcType=VARCHAR}");
        SET("zfb_no = #{record.zfbNo,jdbcType=VARCHAR}");
        
        SpExample example = (SpExample) parameter.get("example");
        applyWhere(example, true);
        return SQL();
    }

    public String updateByPrimaryKeySelective(Sp record) {
        BEGIN();
        UPDATE("sp");
        
        if (record.getMchId() != null) {
            SET("mch_id = #{mchId,jdbcType=VARCHAR}");
        }
        
        if (record.getCardNo() != null) {
            SET("card_no = #{cardNo,jdbcType=VARCHAR}");
        }
        
        if (record.getDeleteFlag() != null) {
            SET("delete_flag = #{deleteFlag,jdbcType=TINYINT}");
        }
        
        if (record.getName() != null) {
            SET("`name` = #{name,jdbcType=VARCHAR}");
        }
        
        if (record.getService() != null) {
            SET("service = #{service,jdbcType=VARCHAR}");
        }
        
        if (record.getBank() != null) {
            SET("bank = #{bank,jdbcType=VARCHAR}");
        }
        
        if (record.getFactoryClassName() != null) {
            SET("factory_class_name = #{factoryClassName,jdbcType=VARCHAR}");
        }
        
        if (record.getUpdateDate() != null) {
            SET("update_date = #{updateDate,jdbcType=TIMESTAMP}");
        }
        
        if (record.getCreateDate() != null) {
            SET("create_date = #{createDate,jdbcType=TIMESTAMP}");
        }
        
        if (record.getState() != null) {
            SET("`state` = #{state,jdbcType=TINYINT}");
        }
        
        if (record.getFailReason() != null) {
            SET("fail_reason = #{failReason,jdbcType=VARCHAR}");
        }
        
        if (record.getSpUserName() != null) {
            SET("sp_user_name = #{spUserName,jdbcType=VARCHAR}");
        }
        
        if (record.getZfbNo() != null) {
            SET("zfb_no = #{zfbNo,jdbcType=VARCHAR}");
        }
        
        WHERE("id = #{id,jdbcType=BIGINT}");
        
        return SQL();
    }

    protected void applyWhere(SpExample example, boolean includeExamplePhrase) {
        if (example == null) {
            return;
        }
        
        String parmPhrase1;
        String parmPhrase1_th;
        String parmPhrase2;
        String parmPhrase2_th;
        String parmPhrase3;
        String parmPhrase3_th;
        if (includeExamplePhrase) {
            parmPhrase1 = "%s #{example.oredCriteria[%d].allCriteria[%d].value}";
            parmPhrase1_th = "%s #{example.oredCriteria[%d].allCriteria[%d].value,typeHandler=%s}";
            parmPhrase2 = "%s #{example.oredCriteria[%d].allCriteria[%d].value} and #{example.oredCriteria[%d].criteria[%d].secondValue}";
            parmPhrase2_th = "%s #{example.oredCriteria[%d].allCriteria[%d].value,typeHandler=%s} and #{example.oredCriteria[%d].criteria[%d].secondValue,typeHandler=%s}";
            parmPhrase3 = "#{example.oredCriteria[%d].allCriteria[%d].value[%d]}";
            parmPhrase3_th = "#{example.oredCriteria[%d].allCriteria[%d].value[%d],typeHandler=%s}";
        } else {
            parmPhrase1 = "%s #{oredCriteria[%d].allCriteria[%d].value}";
            parmPhrase1_th = "%s #{oredCriteria[%d].allCriteria[%d].value,typeHandler=%s}";
            parmPhrase2 = "%s #{oredCriteria[%d].allCriteria[%d].value} and #{oredCriteria[%d].criteria[%d].secondValue}";
            parmPhrase2_th = "%s #{oredCriteria[%d].allCriteria[%d].value,typeHandler=%s} and #{oredCriteria[%d].criteria[%d].secondValue,typeHandler=%s}";
            parmPhrase3 = "#{oredCriteria[%d].allCriteria[%d].value[%d]}";
            parmPhrase3_th = "#{oredCriteria[%d].allCriteria[%d].value[%d],typeHandler=%s}";
        }
        
        StringBuilder sb = new StringBuilder();
        List<Criteria> oredCriteria = example.getOredCriteria();
        boolean firstCriteria = true;
        for (int i = 0; i < oredCriteria.size(); i++) {
            Criteria criteria = oredCriteria.get(i);
            if (criteria.isValid()) {
                if (firstCriteria) {
                    firstCriteria = false;
                } else {
                    sb.append(" or ");
                }
                
                sb.append('(');
                List<Criterion> criterions = criteria.getAllCriteria();
                boolean firstCriterion = true;
                for (int j = 0; j < criterions.size(); j++) {
                    Criterion criterion = criterions.get(j);
                    if (firstCriterion) {
                        firstCriterion = false;
                    } else {
                        sb.append(" and ");
                    }
                    
                    if (criterion.isNoValue()) {
                        sb.append(criterion.getCondition());
                    } else if (criterion.isSingleValue()) {
                        if (criterion.getTypeHandler() == null) {
                            sb.append(String.format(parmPhrase1, criterion.getCondition(), i, j));
                        } else {
                            sb.append(String.format(parmPhrase1_th, criterion.getCondition(), i, j,criterion.getTypeHandler()));
                        }
                    } else if (criterion.isBetweenValue()) {
                        if (criterion.getTypeHandler() == null) {
                            sb.append(String.format(parmPhrase2, criterion.getCondition(), i, j, i, j));
                        } else {
                            sb.append(String.format(parmPhrase2_th, criterion.getCondition(), i, j, criterion.getTypeHandler(), i, j, criterion.getTypeHandler()));
                        }
                    } else if (criterion.isListValue()) {
                        sb.append(criterion.getCondition());
                        sb.append(" (");
                        List<?> listItems = (List<?>) criterion.getValue();
                        boolean comma = false;
                        for (int k = 0; k < listItems.size(); k++) {
                            if (comma) {
                                sb.append(", ");
                            } else {
                                comma = true;
                            }
                            if (criterion.getTypeHandler() == null) {
                                sb.append(String.format(parmPhrase3, i, j, k));
                            } else {
                                sb.append(String.format(parmPhrase3_th, i, j, k, criterion.getTypeHandler()));
                            }
                        }
                        sb.append(')');
                    }
                }
                sb.append(')');
            }
        }
        
        if (sb.length() > 0) {
            WHERE(sb.toString());
        }
    }
}